package com.learn.project2.controller.admin;

import com.learn.project2.model.bo.admin.storage.StorageDeleteBO;
import com.learn.project2.model.bo.admin.storage.StorageUpdateBO;
import com.learn.project2.model.vo.admin.storage.StorageCreateVO;
import com.learn.project2.model.vo.admin.storage.StorageListVO;
import com.learn.project2.service.admin.storage.StorageService;
import com.learn.project2.utils.Result;
import com.sun.xml.internal.bind.v2.TODO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Enumeration;

/**
 * @Classname StorageController
 * @Description TODO 对象存储 67-70
 * @author王茁
 * @Date 2022/1/7 17:07
 */
@RestController()
@RequestMapping("admin/storage")
public class StorageController {

    @Autowired
    StorageService storageService;

    @RequestMapping("list")
    /**
     * @description:
     * @author wangzhuo
     * @date 2022-01-07 17:11
     * @version 1.0
     */
    public Result list(Integer page, Integer limit, Integer key, String name, String sort, String order) {
        StorageListVO list = storageService.list(page, limit, key, name, sort, order);
        return Result.ok(list);
    }

    @PostMapping(value = "create")
    /**
     * @description: TODO 上传图片
     * @author wangzhuo
     * @date 2022-01-07 19:27
     * @version 1.0
     */
    public Result create(@RequestParam("file") MultipartFile file) throws UnknownHostException {
        //获取本机ip
        //String host = InetAddress.getLocalHost().getHostAddress();

        //升级，适配mac系统的获取本机ip；
        String ipHostAddress = null ;
        try {
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress ip = (InetAddress) addresses.nextElement();
                    if (ip instanceof Inet4Address
                            && !ip.isLoopbackAddress() //loopback地址即本机地址，IPv4的loopback范围是127.0.0.0 ~ 127.255.255.255
                            && !ip.getHostAddress().contains(":")) {
                        ipHostAddress = ip.getHostAddress();
                        System.out.println(ipHostAddress);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
            StorageCreateVO storageCreateVO = storageService.create(file,ipHostAddress);
            return Result.ok(storageCreateVO);
    }

    @PostMapping("update")
    /**
     * @description:
     * @author wangzhuo
     * @date 2022-01-07 19:28
     * @version 1.0
     */
    public Result update(@RequestBody StorageUpdateBO storageUpdateBO) {
        StorageUpdateBO updateBO = storageService.update(storageUpdateBO);
        return Result.ok(updateBO);
    }

    @PostMapping("delete")
    /**
     * @description:
     * @author wangzhuo
     * @date 2022-01-07 19:28
     * @version 1.0
     */
    public Result delete(@RequestBody StorageDeleteBO storageDeleteBO) {
        int number = storageService.delete(storageDeleteBO);
        if(number==200){
            return Result.ok();
        }
        return Result.fail("删除失败!!!");
    }
}
